/*
** analyze.c for analyze in /home/vermel/local/epitech/tek2/c/ftrace/pullup-project
** 
** Made by nathan vermelle
** Login   <vermel_n@epitech.net>
** 
** Started on  Fri Jun 21 11:23:28 2012 nathan vermelle
** Last update Fri Jun 29 14:06:46 2012 nathan vermelle
*/

#include "./../includes/ftrace.h"

char		*get_syscall(unsigned int pos)
{
   FILE                          *fd;
   char                          *line = NULL;
   int                           i;
   ssize_t                       read;
   size_t                        len;

   i = 0;
   fd = fopen("./syscall_list.txt", "r");
   if (!fd)
      merror("fopen fail");
   while ((read = getline(&line, &len, fd)) != -1)
   {
      if (i == (int)pos)
      {
	 fclose(fd);
	 return (line);
      }
      i++;
   }
   fclose(fd);
   return (NULL);
}

int		checkSysCalls(struct user_regs_struct regs, unsigned long Lrip)
{
   if (SYSCALL(Lrip))
   {
      if (regs.rax < 1 && regs.rax > 255)
	 return (0);
      else
      {
	 printf("%s", get_syscall(regs.rax));
	 return (1);
      }
   }
   return (0);
}

int		analyzeProc(pid_t pid, struct user_regs_struct regs)
{
   long Lrip;

   if ((Lrip = (unsigned long)ptrace(PTRACE_PEEKTEXT, pid, regs.rip, 0)) != -1)
   {
      if (checkSysCalls(regs, Lrip))
/*GO GO GO*/
	 return 1234567788;
   }
   return 9022902;
}
